package sunbao.ido.config;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * \* User: MeiZhongHao
 * \* Date: 2018-11-12
 * \* Time: 17:13
 * \* Description:
 * \
 */
public class ConfigClient {

    private static Properties CONFIGS;

    private static String propertiesUrl = "src/main/resources/custom.properties";

    static {
        init();
    }

    public static void init() {
        synchronized (ConfigClient.class) {
            doInit();
        }
    }

    private static void doInit() {
        Properties configs = new Properties();
        try (FileInputStream in = new FileInputStream(propertiesUrl)) {
            configs.load(in);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            CONFIGS = configs;
        }
    }

    public static Properties getProperties() {
        return CONFIGS;
    }

    public static String getString(String key) {
        return CONFIGS.getProperty(key);
    }

    private static final ConcurrentMap<String, ConfigChangeListener> LISTENERS;

    private static final ExecutorService CONFIG_CHANGE_EXECUTOR;

    static {
        LISTENERS = new ConcurrentHashMap();
        CONFIG_CHANGE_EXECUTOR = Executors.newFixedThreadPool(10, new ThreadFactory() {
            private AtomicInteger threadIndex = new AtomicInteger(0);

            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, String.format("ConfigChangeListener%d", new Object[]{Integer.valueOf(this.threadIndex.incrementAndGet())}));
            }
        });
    }

    /**
     * 添加事件
     *
     * @param listener ConfigChangeListener
     */
    public static void addConfigChangeListener(ConfigChangeListener listener) {
        if (listener != null) {
            LISTENERS.put(listener.getClass().getName(), listener);
        }
    }

    public static void addConfigChangeListener(List<ConfigChangeListener> listeners) {
        if (listeners != null) {
            Iterator var1 = listeners.iterator();

            while (var1.hasNext()) {
                ConfigChangeListener listener = (ConfigChangeListener) var1.next();
                addConfigChangeListener(listener);
            }

        }
    }

    /**
     * 移除事件
     *
     * @param listener ConfigChangeListener
     */
    public static void removeConfigChangeEventListener(ConfigChangeListener listener) {
        if (listener != null) {
            LISTENERS.remove(listener.getClass().getName());
        }
    }

    /**
     * 通知所有的ConfigChangeListener
     */
    public static void notifyListeners(ConfigChangedEvent event) {
        if (!ConfigClient.LISTENERS.isEmpty()) {
            final CountDownLatch doneSignal = new CountDownLatch(ConfigClient.LISTENERS.size());
            Iterator var12 = ConfigClient.LISTENERS.values().iterator();

            while (var12.hasNext()) {
                final ConfigChangeListener listener = (ConfigChangeListener) var12.next();
                ConfigClient.CONFIG_CHANGE_EXECUTOR.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            listener.onConfigChanged(event);
                        } catch (Throwable var5) {
                            System.out.println("ConfigChangeListener change handle error, name=" + this.getClass().getSimpleName());
                        } finally {
                            doneSignal.countDown();
                        }
                    }
                });
            }

            try {
                doneSignal.await();
            } catch (InterruptedException var9) {
                Thread.currentThread().interrupt();
                System.out.println("ConfigChangeListener change handle  thread interrupted");
            }
        }
    }

}